/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.hazard.om.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopes.bizz.basic.common.api.feign.RemoteParkBasicDictService;
import com.hopes.bizz.basic.hazard.source.entity.HazHazardSourceEntity;
import com.hopes.bizz.basic.hazard.source.entity.HazMonitorFactorEntity;
import com.hopes.bizz.hazard.om.dto.MonitorDataPageDTO;
import com.hopes.bizz.hazard.om.entity.HazRtDataEntity;
import com.hopes.bizz.hazard.om.entity.HazRtDataHisEntity;
import com.hopes.bizz.hazard.om.mapper.HazRtDataMapper;
import com.hopes.bizz.hazard.om.service.HazRtDataHisService;
import com.hopes.bizz.hazard.om.service.HazRtDataService;
import com.hopes.bizz.hazard.om.vo.HazAnalysisDataVO;
import com.hopes.bizz.hazard.om.vo.MonitorDataPageVO;
import com.hopes.boss.common.core.constant.SecurityConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 危险源实时监控数据
 *
 * @author daijie
 * @date 2023-11-02 15:18:20
 */
@Service
public class HazRtDataServiceImpl extends ServiceImpl<HazRtDataMapper, HazRtDataEntity> implements HazRtDataService {

    @Autowired
    private HazRtDataHisService hazRtDataHisService;
    @Autowired
    private RemoteParkBasicDictService remoteParkBasicDictService;

    @Override
    public Page getMonitorDataPage(Page page, MonitorDataPageDTO monitorDataPageDTO) {
        Page<MonitorDataPageVO> monitorDataPage = this.baseMapper.getMonitorDataPage(page, monitorDataPageDTO);
        for (MonitorDataPageVO record : monitorDataPage.getRecords()) {
            HazMonitorFactorEntity monitorFactor = remoteParkBasicDictService.getHazMonitorFactorById(record.getMonitorFactorId(), SecurityConstants.FROM_IN).getData();
            if (monitorFactor != null) {
                record.setStorageId(monitorFactor.getStorageId());
                record.setFactorName(monitorFactor.getFactorName());
                record.setUseStatus(monitorFactor.getUseStatus());
            }

            HazHazardSourceEntity hazHazardSource = remoteParkBasicDictService.getHazHazardSourceById(record.getHazardSourceId(), SecurityConstants.FROM_IN).getData();
            if (hazHazardSource != null) {
                record.setHazardName(hazHazardSource.getHazardName());
                record.setHazardSourceLevel(hazHazardSource.getHazardSourceLevel());
            }
        }

        return monitorDataPage;
    }

    @Override
    public List<MonitorDataPageVO> gisMonitorData() {
        List<MonitorDataPageVO> list = this.baseMapper.gisMonitorData();
        for (MonitorDataPageVO vo : list) {
            HazMonitorFactorEntity monitorFactor = remoteParkBasicDictService.getHazMonitorFactorById(vo.getMonitorFactorId(), SecurityConstants.FROM_IN).getData();
            if (monitorFactor != null) {
                vo.setStorageId(monitorFactor.getStorageId());
                vo.setFactorName(monitorFactor.getFactorName());
                vo.setUseStatus(monitorFactor.getUseStatus());
            }

            HazHazardSourceEntity hazHazardSource = remoteParkBasicDictService.getHazHazardSourceById(vo.getHazardSourceId(), SecurityConstants.FROM_IN).getData();
            if (hazHazardSource != null) {
                vo.setHazardName(hazHazardSource.getHazardName());
                vo.setHazardSourceLevel(hazHazardSource.getHazardSourceLevel());
            }
        }

        return list;
    }

    @Override
    public HazAnalysisDataVO trendAnalysis(HazHazardSourceEntity hazHazardSource) {
        // 时间集合
        ArrayList<String> timeList = new ArrayList<>();
        // 因子的报警限值
//        JSONObject limitAlarm = selectLimitAlarm(pollRtDataDTO);
        String dataSign = "实时";

        List<HazRtDataHisEntity> hisData = hazRtDataHisService.list(
                        Wrappers.<HazRtDataHisEntity>lambdaQuery().eq(HazRtDataHisEntity::getHazardSourceId, hazHazardSource.getId())
                )
                .stream()
                .filter(item -> timeList.add(item.getDataTimeStr()))
                .collect(Collectors.toList());
        List<Double> data = buildData(hisData);
        HazAnalysisDataVO pollAnalysisDataVO = new HazAnalysisDataVO();
        pollAnalysisDataVO.setDataList(data);
        pollAnalysisDataVO.setTimeList(timeList);
        pollAnalysisDataVO.setName(dataSign + "数据趋势");
//        pollAnalysisDataVO.setLimitAlarm(limitAlarm);
//        pollAnalysisDataVO.setName(limitAlarm.getString("name") + dataSign + "数据趋势");
        return pollAnalysisDataVO;
    }

    private List<Double> buildData(List<HazRtDataHisEntity> hisData) {
        ArrayList<Double> list = new ArrayList<>();
        for (HazRtDataHisEntity hisDatum : hisData) {
            BigDecimal val = hisDatum.getMonitorData();
            list.add(val.doubleValue());
        }
        return list;
    }
}